package com.xudow.app.newui;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.course.CourseSetUpItem;
import com.xudow.app.R;
import com.xudow.app.newui.adapter.MyVPAdapter;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.task.SupervisionListTask;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

public class TeachSupervisionActivity extends BaseUiActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private String courseId;
    private final int PAGE_ONE=1;
    private final int PAGE_TWO=2;
    private final int PAGE_THR=3;
    private final int PAGE_FOU=4;
    @BindView(R.id.title_1)
    TextView title_1;

    @BindView(R.id.bottom_1)
    TextView bottom_1;

    @BindView(R.id.title_2)
    TextView title_2;

    @BindView(R.id.bottom_2)
    TextView bottom_2;

    @BindView(R.id.title_3)
    TextView title_3;

    @BindView(R.id.bottom_3)
    TextView bottom_3;

    @BindView(R.id.title_4)
    TextView title_4;

    @BindView(R.id.bottom_4)
    TextView bottom_4;

    @BindView(R.id.vp)
    ViewPager vp;
    MyVPAdapter myVPAdapter;
    int page;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teach_supervision);
        page=getIntent().getIntExtra("page",0);

    }

    @Override
    protected void initData() {

        courseId=getIntent().getStringExtra("courseId");

        HashMap<String,String> map=new HashMap<String,String>();
        map.put("courseid", courseId );
        SupervisionListTask task=new SupervisionListTask(this,handler);
        task.execute(map);
        showLodingDialog("正在加载");
    }

    @Override
    protected void onResume() {
        super.onResume();



    }

    /**
     *督查信息handler
     */
    private List<CourseSetUpItem> setups;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();
            super.handleMessage(msg);
            if (msg.what== SupervisionListTask.SUCCESS){
                setups=(List<CourseSetUpItem>)msg.obj;
                if (null!=setups){
                    myVPAdapter=new MyVPAdapter(getSupportFragmentManager());
                    myVPAdapter.setData(setups);
                    vp.setAdapter(myVPAdapter);
                    switch (page){

                        case PAGE_ONE:
                            title_1.performClick();
                            break;
                        case PAGE_TWO:
                            title_2.performClick();
                            break;
                        case PAGE_THR:
                            title_3.performClick();
                            break;
                        case PAGE_FOU:
                            title_4.performClick();
                            break;
                    }
                }
            }else
                getMyApp().makeToast(msg.getData().getString("errMessage"));

        }
    };

    public void onBackBtnClick(View view) {
        finish();
    }
    @Override
    protected void initListener() {
        title_1.setOnClickListener(this);
        title_2.setOnClickListener(this);
        title_3.setOnClickListener(this);
        title_4.setOnClickListener(this);
        vp.addOnPageChangeListener(this);
    }
    private void cleanTitle(){
        title_1.setTextColor(Color.parseColor("#666666"));
        bottom_1.setVisibility(View.INVISIBLE);
        title_2.setTextColor(Color.parseColor("#666666"));
        bottom_2.setVisibility(View.INVISIBLE);
        title_3.setTextColor(Color.parseColor("#666666"));
        bottom_3.setVisibility(View.INVISIBLE);
        title_4.setTextColor(Color.parseColor("#666666"));
        bottom_4.setVisibility(View.INVISIBLE);
    }
    @Override
    public void onClick(View v) {
        cleanTitle();
        switch (v.getId()){
            case R.id.title_1:
                title_1.setTextColor(Color.parseColor("#159933"));
                bottom_1.setVisibility(View.VISIBLE);
                vp.setCurrentItem(0);
                break;
            case R.id.title_2:
                title_2.setTextColor(Color.parseColor("#159933"));
                bottom_2.setVisibility(View.VISIBLE);
                vp.setCurrentItem(1);
                break;
            case R.id.title_3:
                title_3.setTextColor(Color.parseColor("#159933"));
                bottom_3.setVisibility(View.VISIBLE);
                vp.setCurrentItem(2);
                break;
            case R.id.title_4:
                title_4.setTextColor(Color.parseColor("#159933"));
                bottom_4.setVisibility(View.VISIBLE);
                vp.setCurrentItem(3);
                break;
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state==2){
            cleanTitle();
            switch (vp.getCurrentItem()){
                case 0:
                    title_1.setTextColor(Color.parseColor("#159933"));
                    bottom_1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    title_2.setTextColor(Color.parseColor("#159933"));
                    bottom_2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    title_3.setTextColor(Color.parseColor("#159933"));
                    bottom_3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    title_4.setTextColor(Color.parseColor("#159933"));
                    bottom_4.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }


}
